package com.example.shop.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.api.ServiceStateCode;
import com.example.shop.common.bo.maplocation.InverseLocationInfo;
import com.example.shop.common.bo.maplocation.Location;
import com.example.shop.common.service.MapLocationService;
import com.example.shop.common.util.FileOptionUtil;
import com.example.shop.mapper.UserAddressBookMapper;
import com.example.shop.pojo.User;
import com.example.shop.pojo.UserAddressBook;
import com.example.shop.user.dto.AddLinkmanParam;
import com.example.shop.user.vo.LinkmanInfo;
import com.example.shop.user.dto.ModifyLinkmanParam;
import com.example.shop.user.service.LinkmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class LinkmanServiceImpl implements LinkmanService {

    @Autowired
    MapLocationService mapLocationService;
    @Autowired
    UserAddressBookMapper addressBookMapper;


    @Override
    public ServiceReturn<LinkmanInfo> addLinkman(User user, AddLinkmanParam addInfo) {
        //获取地址簿
        UserAddressBook addressBook = addInfo.getUserAddressBook();
        addressBook.setUserId(user.getUserId());

        InverseLocationInfo inverseLocationInfo = mapLocationService.getLocationInfo(addressBook.getLinkmanArea());
        //如果精度过小，则直接返回
        if(inverseLocationInfo.getLevel().isDifficultLocate()){
            return new ServiceReturn<>(ServiceStateCode.DIFFICULT_LOCATE,null);
        }
        if(!inverseLocationInfo.HighUnderstandPractical()){
            return new ServiceReturn<>(ServiceStateCode.CANT_UNDERSTAND_LOCATE,null);
        }
        Location location = inverseLocationInfo.getLocation();
        addressBook.setLinkmanLatitude(location.getLat());
        addressBook.setLinkmanLongitude(location.getLng());

        String savePath = user.getUserResourceDir() + addressBook.getLinkmanId() + ".png";
        //设置联系人头像的保存路径
        addressBook.setLinkmanPicture(savePath);
        try {
            FileOptionUtil.asyncWrite(savePath,addInfo.getLinkmanPicture().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        int changeRow = addressBookMapper.insert(addressBook);
        if(changeRow!=0){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,LinkmanInfo.getLinkmanInfo(addressBook));
        }
        return new ServiceReturn<>(ServiceStateCode.FAILURE,null);
    }

    @Override
    public ServiceReturn<Boolean> deleteLinkman(User user,long linkmanId) {
        QueryWrapper<UserAddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("linkman_id",linkmanId)
                .eq("user_id",user.getUserId());
        int deleteRow = addressBookMapper.delete(queryWrapper);
        if(deleteRow!=0){
            String deletePath = user.getUserResourceDir() + linkmanId + ".png";
            FileOptionUtil.asyncDeleteFile(deletePath);
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,true);
        }
        return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,false);
    }

    @Override
    public ServiceReturn<LinkmanInfo> getLinkman(User user,long linkmanId) {
        QueryWrapper<UserAddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId())
                .eq("linkman_id",linkmanId);
        UserAddressBook addressBook = addressBookMapper.selectOne(queryWrapper);
        if(addressBook!=null){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,LinkmanInfo.getLinkmanInfo(addressBook));
        }
        return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,null);
    }

    @Override
    public ServiceReturn<List<LinkmanInfo>> getAddressBook(User user) {
        //查询该用户的所有联系人信息
        QueryWrapper<UserAddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        List<UserAddressBook> addressBooks = addressBookMapper.selectList(queryWrapper);
        ArrayList<LinkmanInfo> linkmanInfoList = new ArrayList<>();
        //将联系人转换为视图数据
        for(UserAddressBook addressBook:addressBooks){
            linkmanInfoList.add(LinkmanInfo.getLinkmanInfo(addressBook));
        }
        return new ServiceReturn<>(ServiceStateCode.SUCCESS,linkmanInfoList);
    }

    @Override
    public ServiceReturn<LinkmanInfo> modifyLinkmanInfo(User user, ModifyLinkmanParam modifyInfo) {
        QueryWrapper<UserAddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId())
                .eq("linkman_id",modifyInfo.getLinkmanId());
        //判断该用户是否存在该联系人信息
        boolean exists = addressBookMapper.exists(queryWrapper);
        if(!exists){
            return new ServiceReturn<>(ServiceStateCode.NO_SUCH_RECORD,null);
        }

        UserAddressBook addressBook = modifyInfo.getUserAddressBook();
        //如果要修改该联系人的定位
        if(addressBook.getLinkmanArea()!=null){
            InverseLocationInfo inverseLocationInfo = mapLocationService.getLocationInfo(addressBook.getLinkmanArea());
            //如果精度过小，则直接返回
            if(inverseLocationInfo.getLevel().isDifficultLocate()){
                return new ServiceReturn<>(ServiceStateCode.DIFFICULT_LOCATE,null);
            }
            if(!inverseLocationInfo.HighUnderstandPractical()){
                return new ServiceReturn<>(ServiceStateCode.CANT_UNDERSTAND_LOCATE,null);
            }
            Location location = inverseLocationInfo.getLocation();
            addressBook.setLinkmanLatitude(location.getLat());
            addressBook.setLinkmanLongitude(location.getLng());
        }
        //如果要修改联系人的头像
        if(modifyInfo.getLinkmanPicture()!=null){
            String savePath = user.getUserResourceDir() + addressBook.getLinkmanId() + ".png";
            //设置联系人头像的保存路径
            addressBook.setLinkmanPicture(savePath);
            try {
                FileOptionUtil.asyncWrite(savePath,modifyInfo.getLinkmanPicture().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //进行信息的修改
        int updateRow = addressBookMapper.updateNotNull(addressBook);
        if(updateRow!=0){
            return new ServiceReturn<>(ServiceStateCode.SUCCESS,LinkmanInfo.getLinkmanInfo(addressBook));
        }
        return new ServiceReturn<>(ServiceStateCode.FAILURE,null);
    }
}
